//
//  ChangePwdVC.m
//  ProjectforHDX
//
//  Created by Jaesun on 16/8/2.
//  Copyright © 2016年 H.DX. All rights reserved.
//

#import "ChangePWDVC.h"

#import "ChangePWDInputView.h" // 自定义输入视图

@interface ChangePWDVC ()
/**
 *  旧密码
 */
@property (nonatomic, strong) ChangePWDInputView *oldPWDView;
/**
 *  新密码
 */
@property (nonatomic, strong) ChangePWDInputView *freshPWDView;
/**
 *  确认新密码
 */
@property (nonatomic, strong) ChangePWDInputView *comfrimPWDView;
/**
 *  提交
 */
@property (nonatomic, strong) RZCustomButtom *submitBtn;

@property (nonatomic, strong) NSMutableDictionary *postDic;

@end

@implementation ChangePWDVC

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupViews];
}

#pragma mark 数据初始化
- (void)setupDatas {
    
}

#pragma mark 视图初始化
- (void)setupViews {
    self.navColor = SYSTEM_COLOR;
    
    self.mBackColor = [UtilCheck hexStringToColor:@"f6f7fb" alpha:1];
    // @"修改密码"
    self.labnav.text = [UtilCheck language:@"setting.updatePWD"];
    self.labnav.textColor = [UIColor whiteColor];
    self.labnav.backgroundColor = [UIColor clearColor];
    
    self.oldPWDView = [[ChangePWDInputView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 80)];
    // @"旧密码"
    self.oldPWDView.titleLab.text = [UtilCheck language:@"setting.oldPWD"];

    [self.view addSubview:self.oldPWDView];
    
    self.freshPWDView = [[ChangePWDInputView alloc] initWithFrame:CGRectMake(0, self.oldPWDView.bottom, SCREEN_WIDTH, 80)];
    // @"新密码"
    self.freshPWDView.titleLab.text = [UtilCheck language:@"setting.newPWD"];
 
    [self.view addSubview:self.freshPWDView];
    
    self.comfrimPWDView = [[ChangePWDInputView alloc] initWithFrame:CGRectMake(0, self.freshPWDView.bottom, SCREEN_WIDTH, 80)];
    // @"确认新密码"
    self.comfrimPWDView.titleLab.text = [UtilCheck language:@"setting.confirmPWD"];

    [self.view addSubview:self.comfrimPWDView];
    
    self.submitBtn = [[RZCustomButtom alloc] initWithFrame:CGRectMake(20, self.comfrimPWDView.bottom + 30, SCREEN_WIDTH - 40, 40)];
    // 提交
    [self.submitBtn setTitle:[UtilCheck language:@"setting.submit"] forState:(UIControlStateNormal)];
    self.submitBtn.titleLabel.font = CustomFont(16);
    self.submitBtn.layer.cornerRadius = 5;
    self.submitBtn.backgroundColor = [UtilCheck hexStringToColor:@"6f8cf4" alpha:1];
    [self.submitBtn addTarget:self action:@selector(touchSubmitBtn:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:self.submitBtn];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark- 点击事件
#pragma mark-

#pragma mark 提交
- (void)touchSubmitBtn:(RZCustomButtom *)sender {
   
    [self.oldPWDView.inputText resignFirstResponder];
    [self.freshPWDView.inputText resignFirstResponder];
    [self.comfrimPWDView.inputText resignFirstResponder];
    
    NSString *msg = [self isCanSubmit];
    if ([UtilCheck DataIsNuLL:msg]) {
          self.postDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:self.oldPWDView.inputText.text,@"oldPassword",self.freshPWDView.inputText.text,@"newPassword", nil];
    }
    else {
        [[BDHUD sharedManager] showWords:msg];
        return;
    }

    [self updatePassword];
    
}

#pragma mark- 其他方法
#pragma mark-
/**
 *  检查密码是否合法
 *
 *  @param passWord 密码
 *
 *  @return 合法 YES 不合法 NO
 */
- (BOOL) validatePassword:(NSString *)passWord
{
    NSString *passWordRegex = @"^(?![0-9]{0,24}$)[\\S]{6,16}$";
    
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
    return [passWordPredicate evaluateWithObject:passWord];
}

- (NSString *)isCanSubmit {
    if(self.oldPWDView.inputText.text.length < 6) {
        // 旧密码,不足6位
        return [NSString stringWithFormat:@"%@,%@",[UtilCheck language:@"setting.oldPWD"],[UtilCheck language:@"setting.lessPWD"]];
    }
   else if(self.freshPWDView.inputText.text.length < 6) {
       // 新密码,不足6位
        return [NSString stringWithFormat:@"%@,%@",[UtilCheck language:@"setting.newPWD"],[UtilCheck language:@"setting.lessPWD"]];
    }
   else if(self.comfrimPWDView.inputText.text.length < 6) {
       // 确认新密码,不足6位
       return [NSString stringWithFormat:@"%@,%@",[UtilCheck language:@"setting.confirmPWD"],[UtilCheck language:@"setting.lessPWD"]];
   }
   else if (![self validatePassword:self.oldPWDView.inputText.text]) {
       // 旧密码,不符6-16位,非纯数字,不含空格
        return [NSString stringWithFormat:@"%@,%@",[UtilCheck language:@"setting.oldPWD"],[UtilCheck language:@"setting.limitPWD"]];
    }
    else  if (![self validatePassword:self.freshPWDView.inputText.text]) {
       // 新密码,不符6-16位,非纯数字,不含空格
        return [NSString stringWithFormat:@"%@,%@",[UtilCheck language:@"setting.newPWD"],[UtilCheck language:@"setting.limitPWD"]];
    }
    else   if (![self validatePassword:self.comfrimPWDView.inputText.text]) {
      // 确认新密码,不符6-16位,非纯数字,不含空格
        return [NSString stringWithFormat:@"%@,%@",[UtilCheck language:@"setting.confirmPWD"],[UtilCheck language:@"setting.limitPWD"]];
    }
    else if (![self.freshPWDView.inputText.text isEqualToString:self.comfrimPWDView.inputText.text]) {
     // @"两次新密码不相同"
        return [UtilCheck language:@"setting.noSamePWD"];
    }
    
    return nil;
}

#pragma mark- 网络请求
#pragma mark-

#pragma mark 修改密码
- (void)updatePassword {
    
    [[BDHUD sharedManager] showBDHUD:self.view title:nil];
    [AFHttpTool postJsonDatawithUrl:@"updatePassword"  parameters:self.postDic success:^(id responseObject) {
        
        [[BDHUD sharedManager] dismissHUD];
        if(1 == [[[responseObject objectForKey:@"head"] objectForKey:@"status"] intValue]) {
            
            
            if([[responseObject objectForKey:@"data"] isKindOfClass:[NSDictionary class]]){
                
                NSDictionary *dic = [responseObject objectForKey:@"data"];
                
                [Toolobject showAlterwithMessage:[[responseObject objectForKey:@"head"] objectForKey:@"info"]];
                [UtilCheck dictionarytoModel:dic targetModel:@"THUserInfoModel"]; // 把字典转化为model
                
                [self.navigationController popViewControllerAnimated:YES];

            }
        }
        else {
            //返回数据出问题 弹出服务器返回信息
            responseObject;
            
            if([[[responseObject objectForKey:@"head"] allKeys] containsObject:@"info"]&&![UtilCheck DataIsNuLL:[[responseObject objectForKey:@"head"] objectForKey:@"info"]]){
                [[BDHUD sharedManager] showWords:[[responseObject objectForKey:@"head"] objectForKey:@"info"]];
            }
        }
        
    } fail:^(id responseObject, NSError *error) {
        
        [[BDHUD sharedManager] dismissHUD];
        
        if ([[error.userInfo objectForKey:@"NSLocalizedDescription"] isEqualToString:@"请求超时。"]) {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.time.out"]];
        }else {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.network.interruption"]];
        }
        
    }];
}

@end
